package com.ljh.onlinetest.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljh.onlinetest.base.BaseApiController;
import com.ljh.onlinetest.base.RestResponse;
import com.ljh.onlinetest.domain.Message;
import com.ljh.onlinetest.domain.MessageUser;
import com.ljh.onlinetest.domain.User;
import com.ljh.onlinetest.service.MessageService;
import com.ljh.onlinetest.service.UserService;
import com.ljh.onlinetest.utils.DateTimeUtil;
import com.ljh.onlinetest.utils.PageInfoHelper;
import com.ljh.onlinetest.viewmodel.admin.message.MessageRequestPageVM;
import com.ljh.onlinetest.viewmodel.admin.message.MessageResponseVM;
import com.ljh.onlinetest.viewmodel.admin.message.MessageSendVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ljh
 * @ClassName MessageController.java
 * @createTime 2022年02月22日
 */
@RestController
@RequestMapping("/api/admin/message")
public class MessageController extends BaseApiController {
    private final MessageService messageService;
    private final UserService userService;

    @Autowired
    public MessageController(MessageService messageService, UserService userService) {
        this.messageService = messageService;
        this.userService = userService;
    }

    @PostMapping("page")
    public RestResponse<PageInfo<MessageResponseVM>> page(@RequestBody MessageRequestPageVM model){
        PageInfo<Message> pageInfo = messageService.page(model);
        List<Integer> ids = pageInfo.getList().stream().map(d -> d.getId()).collect(Collectors.toList());
        List<MessageUser> messageUsers = ids.size() == 0 ? null : messageService.selectByMessageIds(ids);
        PageInfo<MessageResponseVM> vmPageInfo = PageInfoHelper.copyMap(pageInfo, message -> {
            MessageResponseVM vm = modelMapper.map(message, MessageResponseVM.class);
            String receives = messageUsers.stream().filter(messageUser -> messageUser.getMessageId().equals(message.getId())).map(MessageUser::getReceiveUserName)
                    .collect(Collectors.joining(","));
            vm.setReceives(receives);
            vm.setCreateTime(DateTimeUtil.dateFormat(message.getCreateTime()));
            return vm;
        });
        return RestResponse.ok(vmPageInfo);
    }

    @PostMapping("send")
    public RestResponse send(@RequestBody @Valid MessageSendVM model){
        User currentUser = getCurrentUser();
        List<User> receiveUsers = userService.selectByIds(model.getReceiveUserIds());
        Date now = new Date();
        Message message = new Message();
        message.setTitle(model.getTitle());
        message.setContent(model.getContent());
        message.setCreateTime(now);
        message.setReadCount(0);
        message.setReceiveUserCount(receiveUsers.size());
        message.setSendUserId(currentUser.getId());
        message.setSendUserName(currentUser.getUserName());
        message.setSendRealName(currentUser.getRealName());

        List<MessageUser> messageUsers = receiveUsers.stream().map(user -> {
            MessageUser messageUser = new MessageUser();
            messageUser.setCreateTime(now);
            messageUser.setReaded(false);
            messageUser.setReceiveUserId(user.getId());
            messageUser.setReceiveUserName(user.getUserName());
            messageUser.setReceiveRealName(user.getRealName());
            return messageUser;
        }).collect(Collectors.toList());
        messageService.sendMessage(message,messageUsers);
        return RestResponse.ok();
    }
}
